package com.iflytek.ci.common.support;

import java.io.File;
import java.util.List;

import com.iflytek.ci.cpms.domain.Milestone;
import com.iflytek.ci.cpms.domain.Project;
import com.iflytek.ci.cpms.service.MilestoneService;
import com.iflytek.ci.cpms.service.ProjectService;
import com.iflytek.ci.main.dto.Prior;
import com.iflytek.ci.qams.domain.Issue;
import com.iflytek.ci.qams.service.IssueService;
import com.iflytek.framework.mapper.JsonMapper;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

/**
 * <p>
 * 定时获取所有未完成的里程碑的反馈信息
 * </p>
 *
 * @author mdzhang
 */
public class GetIssueTask {

	/** 日志 */
	private static Logger logger = LoggerFactory.getLogger(GetIssueTask.class);
	@Autowired
	MilestoneService milestoneService;
	@Autowired
	IssueService issueService;
	@Autowired
	ProjectService projectService;
	@Value("${data_issue_path}")
	String issue_path;

	/** 获取所有里程碑的所有反馈信息 */
	public void getData() {

		List<Milestone> milestone_list = milestoneService.all_milestone();
		for (Milestone milestone : milestone_list) {
			List<Issue> issue_list = issueService
					.issues_with_milestone(milestone.getId());
			Project project = projectService.fromId(milestone.getProject());
			String data = JsonMapper.nonDefaultMapper().toJson(milestone)
					+ JsonMapper.nonDefaultMapper().toJson(issue_list);
			String name = project.getCode() + "-" + milestone.getName()
					+ ".txt";
			try {
				File milestone_issue_file = new File(issue_path + name);// 命名规则为：“项目缩写-里程碑名.txt”，如“cdms-milestone1.txt”
				FileUtils.writeStringToFile(milestone_issue_file, data,
						Prior.DEFAULT_ENCODING);
			} catch (Exception e) {
				logger.debug("创建文件并写入数据失败");
			}
		}
	}
}
